#ifndef _I2CBAR_H_
#define _I2CBAR_H_

#ifndef 	F_CPU
#define 	F_CPU 8000000UL
#endif

#include "irbar.h"
#include "btn.h"
#include "led.h"
#include "ping.h"
#include "buzzer.h"
#include "usi-twi-slave.h"
#include "timer.h"


////////////////////////////////////////////////////////
// PROTOTYPES
////////////////////////////////////////////////////////

void i2cBarSetup(void);
void i2cBarLoop(void);



////////////////////////////////////////////////////////
// PIN LAYOUT
////////////////////////////////////////////////////////
#define INPUT_PORT_DDR  DDRA
#define INPUT_PORT		PORTA
#define INPUT_PORT_PIN  PINA
#define CTRL_PORT_DDR   DDRB
#define CTRL_PORT       PORTB
 
#define PIN_BTN_1       PA0
#define PIN_BTN_2       PA1
#define PIN_PING        PA2
#define PIN_IR_0        PA3
#define PIN_IR_1        PA4
#define PIN_IR_2        PA5
#define PIN_IR_3        PA6
#define PIN_IR_4        PA7


// PORT_USI_SDA         PB0
// unused               PB1
// PORT_USI_SCL         PB2
#define CTRL_BUZZER     PB3
#define CTRL_LED_1      PB4
#define CTRL_LED_2      PB5
#define CTRL_IR_BAR     PB6



////////////////////////////////////////////////////////
// usi-twi protocol definitions
////////////////////////////////////////////////////////


// Note: The LSB is the I2C r/w flag and must not be used for addressing!
#define 	SLAVE_ADDR_ATTINY       0x40

#define I2C_CMD_PING_ENABLE    0x01
#define I2C_CMD_IRBAR_ENABLE   0x02
#define I2C_CMD_LIGHT2_ENABLE  0x04
#define I2C_CMD_LIGHT1_ENABLE  0x08
#define I2C_CMD_SOUND_PLAY1    0x10
#define I2C_CMD_SOUND_PLAY2    0x20
#define I2C_CMD_SOUND_PLAY3    0x40
//#define I2C_CMD_NA             0x80 // free



// the protocol:

// master sends 1 byte command (bits see above):
// bit : 7             |     6..4   |     3..2   |     1         |     0
// val : 0 (reserverd) | sound 3..0 | light 0..1 | ir_bar_enable | ping_enable 


// then, the can read 7 bytes from this slave:

// 0: I2C_RESPONSE_STATUS: b7..2: 0, b1 = btn1-state, b0 = btn0-state
// 1: I2C_IR_BAR1_VALUE
// 2: I2C_IR_BAR2_VALUE
// 3: I2C_IR_BAR3_VALUE
// 4: I2C_IR_BAR4_VALUE
// 5: I2C_IR_BAR5_VALUE
// 6: I2C_PING_VALUE (in cm)


// Sounds to select with I2C_CMD_SOUND_PLAYx:

// 0 off
// 1 c
// 2 e
// 3 g
// 4 c
// 5 battery low
// 6 ir bar audio feedback
// 7 ping audio feedback



////////////////////////////////////////////////////////
// Useful macros
////////////////////////////////////////////////////////
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )



#endif  // ifndef _I2CBAR_H_